package com.zhixun.mobile.modules.jiujiudevice;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.boxin.ims.modules.sys.utils.RandomUtils;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 文件上传
 * @author sxh
 * @version 2018-06-06
 */
@Controller
@RequestMapping(value = Global.FRONT_PATH+"/jiujiudevice/fileUpload")
public class FileUploadController extends BaseController{
	@RequestMapping(value = "up")
	@ResponseBody
	public void save(HttpServletRequest request, MultipartFile file,HttpServletResponse response) {
		System.out.println("进入富文本上传文件的方法");
		String fileName = WechatUtils.getNewsUpPath(request)+File.separator;
		FileUtils.createDirectory(fileName);
		String staff = "";
		String fileKey = "";
		// 上传文件
		if (file.getSize() != 0 ) {
			staff = FilenameUtils.getExtension(file.getOriginalFilename());
			fileKey = UserUtils.getUser().getLoginName()+"_useragree_" +RandomUtils.getRandomName(6)+"."+staff;
			File f = new File(fileName+fileKey);
			System.out.println("保存文件:"+fileName+fileKey);
			 try {
				   file.transferTo(f);
				} catch (Exception e) {
					System.out.println("上传文件失败:"+fileName+fileKey);
					e.printStackTrace();
				}
			Map map = new HashMap();
			map.put("success", true);
			map.put("file_path", fileName+fileKey);
			writeJson(request, response, map);
			return ;
		}
		Map map = new HashMap();
		map.put("success", false);
		map.put("info", "上传失败");
		writeJson(request, response, map);
		return ;
		
	}
}
